package cn.initcap.concurrency.concurrency.atomic;

import cn.initcap.concurrency.annoations.ThreadSafe;
import java.util.concurrent.atomic.AtomicReference;
import lombok.extern.slf4j.Slf4j;

/**
 * @author initcap
 * @date Created in 2018/6/13 PM9:29.
 */
@Slf4j
@ThreadSafe
public class AtomicReferenceExample {

    private static AtomicReference<Integer> count = new AtomicReference<>(0);

    public static void main(String[] args) {
        //当值为0时更新成2
        count.compareAndSet(0, 2);
        //值为2不进行更新
        count.compareAndSet(0, 1);
        //不进行更新
        count.compareAndSet(1, 3);
        //更新为4
        count.compareAndSet(2, 4);
        //不更新
        count.compareAndSet(3, 5);
        //结果为4
        log.info("count:{}", count.get());
    }

}
